import java.util.Arrays;

public class MyStack<E> {
    public  Object[] elem;
    public  int usedSize;
    public static final int Capacity = 5;

    public MyStack() {
        elem = new Object[Capacity];
    }

    //入栈
    public void push(E val){
        if (isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }

    private boolean isFull(){
        return usedSize == elem.length;
    }

    //出栈
    public E pop(E val){
        if (isEmpty()){
            return null;
        }
        E ret = (E) elem[elem.length - 1];
        usedSize--;
        return ret;
    }

    private boolean isEmpty(){
        return usedSize == 0;
    }

    //获取栈顶元素
    public E peek(E val){
        if (isEmpty()){
            return null;
        }
        return (E) elem[elem.length - 1];
    }
}
